const template_id = 'PrtZRQ4YdyG5bHuFLmG8KOgj-OA4wMxTZNlCe72HzTA';  //模板id
//保存用户openid信息
let users = [
  "ovQlH5DYzaYZlRlqOnFZMC0j6QyA", //金世锋大号
  //"ovQlH5OzPI7puDCZIJwinxn8n4mU", //金世锋小号
  "ovQlH5APZagIkqyaNJg-L_3dgT4A", //钟雨萱大号
];
let user; //获取用户openid
var s = new Array(5); //数据信息保存
var amapFile = require('../../lib/amap-wx.130.js');

Page({
  //获取天气
  onLoad: function() {
    this.getGaode()
    this.getLove()
  },
  //整合函数,获取openid,获取授权,发送订阅消息
  getMessage(){
    //获取当前用户openid
    wx.cloud.callFunction({
      name: "getopenid917"
    }).then(res => {
      console.log("获取openid成功", res)
      user = res.result.openid
    }).catch(res => {
      console.log("获取openid失败", res)
      wx.showToast({
        title: '获取openid失败，请速速联系你男人',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    })
    //获取用户授权
    wx.requestSubscribeMessage({
      tmplIds: [template_id],
      success(res){
        console.log("授权成功", res)
        //发送订阅消息
        if (res[template_id] === 'accept') {
          wx.cloud.callFunction({
            name: "fasong",
            data: {
              openid: user,
              name: s[0],
              weather: s[1],
              date: s[2],
              love: s[3],
              hi: s[4],
              templateId: template_id,
            }
          }).then(res => {
            console.log("发送单条消息成功", res)
            wx.showToast({
              title: '订阅成功！明早见哦',
              icon: 'none',
              duration: 2000,
              mask: true
            })
          }).catch(res => {
            console.log("发送单条消息失败", res)
            wx.showToast({
              title: '订阅失败，请速速联系你男人',
              icon: 'none',
              duration: 2000,
              mask: true
            })
          })
        }
      },
      fail(res){
        console.log("授权失败", res)
        wx.showToast({
          title: '授权失败，请速速联系你男人',
          icon: 'succes',
          duration: 2000,
          mask: true
        })
      }
    })
  },
  
  //获取用户openid
  getOpenid(){
    wx.cloud.callFunction({
      name: "getopenid917"
    }).then(res => {
      console.log("获取openid成功", res)
      user = res.result.openid
    }).catch(res => {
      console.log("获取openid失败", res)
    })
  },
  //获取用户授权(并发送订阅消息)
  shouquan(){
    wx.requestSubscribeMessage({
      tmplIds: [template_id],
      success(res){
        console.log("授权成功", res)
      },
      fail(res){
        console.log("授权失败", res)
      }
    })
  },
  //发送消息给单个用户
  sendOne(){
    this.sendFun("ovQlH5DYzaYZlRlqOnFZMC0j6QyA")
  },
  //发送消息给多个用户
  sendAll(){
    let users = [
      "ovQlH5DYzaYZlRlqOnFZMC0j6QyA", //金世锋大号
      "ovQlH5OzPI7puDCZIJwinxn8n4mU", //金世锋小号
    ]
    users.forEach(item=>{
      //console.log("for循环", item)
      this.sendFun(item)
    })
  },
  //封装的发送方法
  sendFun(openid){
    wx.cloud.callFunction({
      name: "fasong",
      data: {
        openid: openid,
        name: s[0],
        weather: s[1],
        date: s[2],
        love: s[3],
        hi: s[4],
        templateId: template_id,
      }
    }).then(res => {
      console.log("发送单条消息成功", res)
    }).catch(res => {
      console.log("发送单条消息失败", res)
    })
  },
  //获取天气
  getGaode(){
    var myAmapFun = new amapFile.AMapWX({key:'7e4a83e14186a4d3d5c25ff553e4ecb3'})
    myAmapFun.getWeather({
      type:'forecast',
      success: function(data){
        //成功回调
        console.log("获取天气信息成功", data.forecast)
        //var temp = data.forecast.casts[1]
        //console.log("获取天气信息成功", temp.date)
        s[0] = "☎金世锋小助手"
        s[1] = "✈ " + data.forecast.province + "市" + data.forecast.city + "\n" + 
        "☁" + data.forecast.casts[1].dayweather + "，" + data.forecast.casts[1].daytemp + "°"
        s[2] = data.forecast.casts[1].date
        s[4] = "今天也要好好学四级哦\n记得给我授权！！！"
      },
      fail: function(info){
        //失败回调
        console.log("获取天气信息失败", info)
      }
    })
  },
  //计算纪念日
  getLove() {
    //这里写开始日期
    var startDate = new Date('Sept 17 2022 00:00:00');
    var timestamp = new Date().getTime();
    var startstamp = startDate.getTime();
    timestamp -= startstamp;
    timestamp /= 1000;
    var day = Math.floor(timestamp / (60 * 60 * 24)) + 1;
    timestamp = Math.floor(timestamp % (60 * 60 * 24));
    //var hour = Math.floor(timestamp / (60 * 60));
    timestamp = Math.floor(timestamp % (60 * 60));
    //var minute = Math.floor(timestamp / 60);
    timestamp = Math.floor(timestamp % 60);
    //var second = timestamp;
    s[3] = "❤我们已经相爱了" + day.toString() + '天'
    //console.log(s[3])
  },
})